package org.lql.chaos.rocketmq.controller;

import org.lql.chaos.rocketmq.service.producer.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author: liangqinglong
 * @date: 2025-08-16 20:19
 * @description: RocketMQ各种消息类型发送控制器
 **/
@RestController
@RequestMapping("/message")
public class MessageProduceController {

	@Autowired
	private NormalProducer normalProducer;

	@Autowired
	private OrderProducer orderProducer;

	@Autowired
	private ScheduleProducer scheduleProducer;

	@Autowired
	private TransactionProducer transactionProducer;

	@Autowired
	private BatchProducer batchProducer;

	/**
	 * 获取消息发送统计信息
	 *
	 * @param businessType 业务类型
	 * @param topic        主题
	 * @return 统计信息
	 */
	@GetMapping("/statistics")
	public String getStatistics(@RequestParam String businessType,
	                            @RequestParam String topic) {
		return org.lql.chaos.rocketmq.service.producer.MonitoredSendCallback.getStatistics(businessType, topic);
	}

	/**
	 * 重置消息发送统计信息
	 *
	 * @param businessType 业务类型
	 * @param topic        主题
	 * @return 重置结果
	 */
	@PostMapping("/statistics/reset")
	public String resetStatistics(@RequestParam String businessType,
	                              @RequestParam String topic) {
		org.lql.chaos.rocketmq.service.producer.MonitoredSendCallback.resetStatistics(businessType, topic);
		return "统计信息已重置";
	}

	/**
	 * 发送普通消息
	 *
	 * @param topic 主题
	 * @param tag   标签
	 * @param msg   消息内容
	 * @return 发送结果
	 */
	@PostMapping("/normal")
	public String sendNormalMessage(@RequestParam String topic,
	                                @RequestParam(required = false, defaultValue = "*") String tag,
	                                @RequestParam String msg) {
		topic = "test-topic";
		tag = "normal";
		msg = "普通消息";
		normalProducer.sendSync(topic, tag, msg);
		return "普通消息发送成功";
	}

	/**
	 * 发送单向消息
	 *
	 * @param topic 主题
	 * @param tag   标签
	 * @param msg   消息内容
	 * @return 发送结果
	 */
	@PostMapping("/oneway")
	public String sendOneWayMessage(@RequestParam String topic,
	                                @RequestParam(required = false, defaultValue = "*") String tag,
	                                @RequestParam String msg) {
		topic = "test-topic";
		tag = "oneway";
		msg = "单向消息";
		normalProducer.sendOneWay(topic, tag, msg);
		return "单向消息发送成功";
	}

	/**
	 * 发送异步消息
	 *
	 * @param topic 主题
	 * @param tag   标签
	 * @param msg   消息内容
	 * @return 发送结果
	 */
	@PostMapping("/async")
	public String sendAsyncMessage(@RequestParam String topic,
	                               @RequestParam(required = false, defaultValue = "*") String tag,
	                               @RequestParam String msg) {
		topic = "test-topic";
		tag = "async";
		msg = "异步消息";
		normalProducer.sendAsync(topic, tag, msg);
		return "异步消息发送请求已提交";
	}

	/**
	 * 发送异步消息 带监控功能的消息发送回调类
	 *
	 * @param topic 主题
	 * @param tag   标签
	 * @param msg   消息内容
	 * @return 发送结果
	 */
	@PostMapping("/async/monitor")
	public String sendAsyncMessageWithMonitor(@RequestParam String topic,
	                               @RequestParam(required = false, defaultValue = "*") String tag,
	                               @RequestParam String msg) {
		topic = "test-topic";
		tag = "async-monitor";
		msg = "异步消息";
		normalProducer.sendAsyncWithMonitor(topic, tag, msg);
		return "异步消息发送请求已提交";
	}

	/**
	 * 发送顺序消息
	 *
	 * @param topic   主题
	 * @param tag     标签
	 * @param msg     消息内容
	 * @param hashKey 用于保证顺序的hash key
	 * @return 发送结果
	 */
	@PostMapping("/order")
	public String sendOrderMessage(@RequestParam String topic,
	                               @RequestParam(required = false, defaultValue = "*") String tag,
	                               @RequestParam String msg,
	                               @RequestParam String hashKey) {
		topic = "test-topic";
		tag = "order";
		msg = "顺序消息";
		hashKey = "order-key";
		orderProducer.sendOrderMessage(topic, tag, msg, hashKey);
		return "顺序消息发送成功";
	}

	/**
	 * 发送延时/定时消息
	 *
	 * @param topic      主题
	 * @param tag        标签
	 * @param msg        消息内容
	 * @param delayLevel 延时级别 (1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h)
	 * @return 发送结果
	 */
	@PostMapping("/delay")
	public String sendDelayMessage(@RequestParam String topic,
	                               @RequestParam(required = false, defaultValue = "*") String tag,
	                               @RequestParam String msg,
	                               @RequestParam(required = false, defaultValue = "3") int delayLevel) {
		topic = "test-topic";
		tag = "delay";
		msg = "定时消息";
		delayLevel = 3;
		scheduleProducer.sendDelayMessage(topic, tag, msg, delayLevel);
		return "延时消息发送成功";
	}

	/**
	 * 发送事务消息
	 *
	 * @param topic 主题
	 * @param tag   标签
	 * @param msg   消息内容
	 * @param arg   事务参数
	 * @return 发送结果
	 */
	@PostMapping("/transaction")
	public String sendTransactionMessage(@RequestParam String topic,
	                                     @RequestParam(required = false, defaultValue = "*") String tag,
	                                     @RequestParam String msg,
	                                     @RequestParam(required = false) String arg) {
		topic = "test-topic";
		tag = "transaction";
		msg = "事务消息";
		arg = "事务参数";
		transactionProducer.sendTransactionMessage(topic, tag, msg, arg);
		return "事务消息发送成功";
	}

	/**
	 * 发送批量消息
	 *
	 * @param topic 主题
	 * @param tag   标签
	 * @param msgs  消息列表
	 * @return 发送结果
	 */
	@PostMapping("/batch")
	public String sendBatchMessage(@RequestParam String topic,
	                               @RequestParam(required = false, defaultValue = "*") String tag,
	                               @RequestBody List<String> msgs) {
		topic = "test-topic";
		tag = "batch";
		msgs = Arrays.asList("批量消息1", "批量消息2", "批量消息3");
		batchProducer.sendBatchMessage(topic, tag, msgs);
		return "批量消息发送成功";
	}
}